package com.duk.util;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataBaseUtil {
	private Context _context;
	private SQLiteDatabase database;
	private DatabaseHelper databaseHelper;
	
	public DataBaseUtil(Context paramContext){
		this._context = paramContext;
	}
	
	//检查这条新闻是否存在,存在返回false 
	public boolean checkExist(String newsId){
		 String existSQL = "select * from favorite where news_id='"+newsId+"'";
		 Cursor cursor = this.database.rawQuery(existSQL, null);
		 if (cursor != null && cursor.moveToFirst()){
			 return true;
		 }
		 return false;
	}
	
	public void closeDataBase()
	{
		if (this.database != null){
			this.database.close();
		}
	}
	
	//删除某一条收藏
	public void deleteByNewsId(String paramString)
	{
		String str = "delete from favorite where news_id='" + paramString + "'";
	    this.database.execSQL(str);
	}
	
	public boolean insertData(HashMap<String, String> data){
		
		if ( checkExist(data.get("news_id")) ){
			deleteByNewsId(data.get("unews_id"));
			return false;
		} else {
			ContentValues cv = new ContentValues();
		    cv.put("news_id", data.get("news_id"));
		    cv.put("title", data.get("title"));
		    cv.put("content", data.get("content"));
		    cv.put("url", data.get("url"));
		    cv.put("image_path", data.get("image_path"));
		    cv.put("news_type",  Integer.valueOf(data.get("news_type")) );
		    cv.put("webdata", data.get("webdata"));
		    cv.put("pubdate", data.get("pubdate"));
		    this.database.insert("favorite", null, cv);
		    return true;
		  
		}
	}
	
	public DataBaseUtil open() throws SQLException
	{
		this.databaseHelper = new DatabaseHelper(this._context,"duknews.db");
		SQLiteDatabase db = this.databaseHelper.getWritableDatabase();
		this.database = db;
		return this;
	}
	
	//取得所有数据，并分类
	public ArrayList<HashMap<String,String>> selectAllData()
	{
		int newsType = 0;
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	    Cursor cursor = this.database.rawQuery("select * from favorite order by news_type", null);

	    if (cursor != null) {
	    	if (cursor.moveToFirst()){
	    		
	    		do{
	    			//设置类别分隔点,标识news_id = category
	    			if(newsType != cursor.getInt(cursor.getColumnIndex("news_type"))){
	    				HashMap<String,String> map1 = new HashMap<String,String>();
		    			map1.put("news_id","category" );
		    			map1.put("news_type",cursor.getString(cursor.getColumnIndex("news_type")));
		    			list.add(map1);
		    			newsType = cursor.getInt(cursor.getColumnIndex("news_type"));
	    			}
	    			HashMap<String,String> map2 = new HashMap<String,String>();
	    			map2.put("news_id", cursor.getString(cursor.getColumnIndex("news_id")) );
	    			map2.put("title", cursor.getString(cursor.getColumnIndex("title")));
	    			map2.put("content",cursor.getString(cursor.getColumnIndex("content")));
	    			map2.put("url",cursor.getString(cursor.getColumnIndex("url")));
	    			map2.put("image_path",cursor.getString(cursor.getColumnIndex("image_path")));
	    			map2.put("news_type",String.valueOf(cursor.getInt(cursor.getColumnIndex("news_type"))));
	    			map2.put("webdata",cursor.getString(cursor.getColumnIndex("webdata")));
	    			map2.put("pubdate",cursor.getString(cursor.getColumnIndex("pubdate")));
	    			list.add(map2);
	    		} while(cursor.moveToNext());
	    	}
	    } 
	
	    cursor.close();
	    return list;
	}
	
	
}
